<?php
/*
  $Id: navigation_history.php,v 1.3 2005/08/18 13:06:44 mwithington Exp $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/

  class navigationHistory {
    var $path, $snapshot, $osMod;

    function navigationHistory() {
      $this->reset();
    }

    function reset() {
      $this->path = array();
      $this->snapshot = array();
    }

    function add_current_page() {
      global $request_type, $catID, $osMod, $_GET, $_POST;

      // We shouldn't be logging actions
      if(isset($_REQUEST['action']))return;
      if($osMod == 'product_thumb')return;

      $set = 'true';
      for ($i=0, $n=sizeof($this->path); $i<$n; $i++) {
        if ( ($this->path[$i]['page'] == ($osMod . '.php')) ) {
          if (isset($catID)) {
            if (!isset($this->path[$i]['get']['catID'])) {
              continue;
            } else {
              if ($this->path[$i]['get']['catID'] == $catID) {
                array_splice($this->path, ($i+1));
                $set = 'false';
                break;
              } else {
                $old_catID = explode('_', $this->path[$i]['get']['catID']);
                $new_catID = explode('_', $catID);

                for ($j=0, $n2=sizeof($old_catID); $j<$n2; $j++) {
                  if ($old_catID[$j] != $new_catID[$j]) {
                    array_splice($this->path, ($i));
                    $set = 'true';
                    break 2;
                  }
                }
              }
            }
          } else {
            $set = 'true';

            array_splice($this->path,($i));

            // Concept to validate unique request
            
            //if( count($this->path[$i]['get']) && count($_GET) )
            //  array_splice($this->path,($i));
            //elseif( count($this->path[$i]['post']) && count($_POST) )
            //  array_splice($this->path,($i));
            
            break;
          }
        }
      }

      if ($set == 'true') {
        $this->path[] = array('page' => ($osMod . '.php'),
                              'mode' => $request_type,
                              'get'  => $this->filter_parameters($_GET),
                              'post' => $this->filter_parameters($_POST));
      }
    }
    
    // Returns Values Array of a path (either GET or POST)
    function get_path_values( $path_id ){
      if(count($this->path[$path_id]['get']))
        return $this->path[$path_id]['get'];
      elseif(count($this->path[$path_id]['post']))
            return $this->path[$path_id]['post'];
      return Array();
    } // - END get_path_values -
    
    // Returns Specific Value of a path (either GET or POST)
    function get_path_value( $path_id, $key_name ){
      if(isset($this->path[$path_id]['get'][$key_name]))
        return $this->path[$path_id]['get'][$key_name];
      elseif(isset($this->path[$path_id]['post'][$key_name]))
            return $this->path[$path_id]['post'][$key_name];
      return NULL;
    } // - END get_path_value -
    
    function remove_current_page() {
      global $osMod;
      $last_entry_position = sizeof($this->path) - 1;
      if ($this->path[$last_entry_position]['page'] == ($osMod . '.php')) {
        unset($this->path[$last_entry_position]);
      }
    }

    function set_snapshot($page = '') {
      global $request_type, $osMod, $_GET, $_POST;
      if (is_array($page)) {
        $this->snapshot = array('page' => $page['page'],
                                'mode' => $page['mode'],
                                'get' => $this->filter_parameters($page['get']),
                                'post' => $this->filter_parameters($page['post']));
      } else {
        $this->snapshot = array('page' => ($osMod . '.php'),
                                'mode' => $request_type,
                                'get'  => $this->filter_parameters($_GET),
                              'post' => $this->filter_parameters($_POST));
      }
    }

    function clear_snapshot() {
      $this->snapshot = array();
    }

    function set_path_as_snapshot($history = 0) {
      $pos = (sizeof($this->path)-1-$history);
      $this->snapshot = array('page' => $this->path[$pos]['page'],
                              'mode' => $this->path[$pos]['mode'],
                              'get'  => $this->path[$pos]['get'],
                              'post' => $this->path[$pos]['post']
                              );
    }

    function debug() {
      for ($i=0, $n=sizeof($this->path); $i<$n; $i++) {
        echo $this->path[$i]['page'] . '?';
        while (list($key, $value) = each($this->path[$i]['get'])) {
          echo $key . '=' . $value . '&';
        }
        if (sizeof($this->path[$i]['post']) > 0) {
          echo '<br>';
          while (list($key, $value) = each($this->path[$i]['post'])) {
            echo '&nbsp;&nbsp;<b>' . $key . '=' . $value . '</b><br>';
          }
        }
        echo '<br>';
      }
      if (sizeof($this->snapshot) > 0) {
        echo '<br><br>';
        echo $this->snapshot['mode'] . ' ' . $this->snapshot['page'] . '?' . tep_array_to_string($this->snapshot['get'], array(tep_session_name())) . '<br>';
      }
    }

    function filter_parameters($parameters) {
	  $clean = array();
	  if (is_array($parameters)) { 
           reset($parameters); 
           while (list($key, $value) = each($parameters)) { 
             if (strpos($key, '_nh-dns') < 1) { 
               $clean[$key] = $value; 
             } 

	  //1199 start
	  //reset($parameters);
	  //while (list($key, $value) = each($parameters)) {
	   // if (strpos($key, '_nh-dns') < 1) {
		 // $clean[$key] = $value;
		 
		 
		}
	  }
	  
	  return $clean;
	  
	}

    function unserialize($broken) {
      for(reset($broken);$kv=each($broken);) {
        $key=$kv['key'];
        if (gettype($this->$key)!="user function")
        $this->$key=$kv['value'];
      }
    }
  }


